Imports System.Windows.Forms
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports clsLookup
Imports clsLookup.clsLookup
Imports SQLUtil.SQL

Public Class clsGetcodes

#Region " Declare "
    Public oLookup As zLookup
    Private oCode As TextBox, oConn, oSysConn As SqlConnection, cTable, cField, cId As String
    Private mEnabled As Boolean = True, mKeyFilter As String
    Private mSelect As Button
    Private xAddXCtrl As addXCtrl

    Public ReadOnly Property DtSrc() As DataView
        Get
            Return oLookup.xdv
        End Get
    End Property

    Public Property oEnabled() As Boolean
        Get
            Return mEnabled
        End Get
        Set(ByVal Value As Boolean)
            mEnabled = Value
            oCode.Enabled = Value
        End Set
    End Property

    Public Property oKeyFilter() As String
        Get
            Return mKeyFilter
        End Get
        Set(ByVal Value As String)
            mKeyFilter = Value
            oLookup.KeyFilter = Value
            xAddXCtrl.xAC.lKey = Value
        End Set
    End Property

#End Region

#Region " New "
    Sub New(ByVal txtCode As TextBox, ByVal appConn As SqlConnection, ByVal sTable As String, ByVal sFieldCode As String, ByVal sId As String, ByVal sKey As String, ByVal oParent As Control)
        oCode = txtCode
        oConn = appConn
        oSysConn = appConn
        cTable = sTable
        cField = sFieldCode
        cId = sId
        mKeyFilter = sKey
        oLookup = New zLookup(oSysConn, sId, mKeyFilter, txtCode.ReadOnly)
        oLookup.RefreshDtSrc(oConn, txtCode.Text)
        oLookup.FRecordable = ""
        xAddXCtrl = New addXCtrl(txtCode, oConn, sTable, sFieldCode, sKey)
        AddHandler xAddXCtrl.cmd_Click, AddressOf oSelectClick
    End Sub
#End Region

#Region " Sub "
    Private Sub oSelectClick(ByVal sender As Object, ByVal e As System.EventArgs)
        If oCode.ReadOnly Then
            oLookup.zLookup(oConn, oCode.Text)
        Else
            oCode.Text = oLookup.zLookup(oConn, oCode.Text)
        End If
        SendKeys.Send("{TAB}")
    End Sub
#End Region
End Class

#Region " Class addXCtrl "
Public Class addXCtrl
    Private ltxt As TextBox
    Private ltxtX As TextBox, lcmdX As Button
    Public Event cmd_Click(ByVal sender As Object, ByVal e As EventArgs)
    Public xAC As AC
    Public Sub New(ByVal txt As TextBox, ByVal appConn As SqlClient.SqlConnection, ByVal tbl As String, ByVal fieldcode As String, ByVal ckey As String)
        ltxt = txt
        ltxtX = New TextBox
        With ltxtX
            .Width = ltxt.Width - 24
            .BorderStyle = BorderStyle.None
            .ReadOnly = ltxt.ReadOnly
            .Enabled = ltxt.Enabled
            .TabStop = ltxt.TabStop
            .Top = 1
            .Left = 1
            .Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        End With
        ltxt.ForeColor = ltxt.BackColor
        ltxt.Controls.Add(ltxtX)
        AddHandler ltxt.Enter, AddressOf ltxt_Enter
        AddHandler ltxt.TextChanged, AddressOf ltxt_TextChanged
        AddHandler ltxt.ReadOnlyChanged, AddressOf ltxt_ReadOnlyChanged
        AddHandler ltxtX.LostFocus, AddressOf ltxtX_LostFocus
        lcmdX = New Button
        With lcmdX
            .Width = 16
            .Height = 14
            .Text = "..."
            .Top = 1
            .Left = ltxt.Width - 21
            .TabStop = False
            .Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            .FlatStyle = FlatStyle.Popup
            .BackColor = System.Drawing.SystemColors.Control
            .ForeColor = System.Drawing.SystemColors.ControlText
            .TextAlign = ContentAlignment.TopCenter
            .Font = New System.Drawing.Font(ltxt.Font.Name, 5, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            .Cursor = System.Windows.Forms.Cursors.Default
        End With
        ltxt.Controls.Add(lcmdX)
        AddHandler lcmdX.Click, AddressOf lcmdX_Click
        xAC = New AC(ltxtX, appConn, tbl, fieldcode, ckey)
        AddHandler ltxtX.KeyDown, AddressOf ltxtX_KeyDown
        ltxt_TextChanged(ltxt, New EventArgs)
    End Sub

    Private Sub ltxt_Enter(ByVal sender As Object, ByVal e As EventArgs)
        ltxtX.Focus()
        ltxtX.SelectionStart = 0
    End Sub

    Private Sub ltxt_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
        ltxtX.Text = sender.Text
    End Sub

    Private Sub ltxt_ReadOnlyChanged(ByVal sender As Object, ByVal e As EventArgs)
        ltxtX.ReadOnly = sender.ReadOnly
    End Sub

    Private Sub ltxtX_LostFocus(ByVal sender As Object, ByVal e As EventArgs)
        ltxt.Text = sender.Text
        If sender.FindForm.ActiveControl Is sender.Parent Then
            SendKeys.Send("+{TAB}")
        End If
    End Sub

    Private Sub lcmdX_Click(ByVal sender As Object, ByVal e As EventArgs)
        ltxt.Text = ltxtX.Text
        RaiseEvent cmd_Click(sender, e)
    End Sub

    Private Sub ltxtX_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
        'If e.KeyCode = Keys.Space And Not (e.Alt Or e.Control Or e.Shift) Then
        '    e.Handled = True
        '    RaiseEvent cmd_Click(lcmdX, New EventArgs)
        'End If
        If e.KeyCode = Keys.F5 And Not (e.Alt Or e.Control Or e.Shift) Then
            e.Handled = True
            ltxt.Text = ltxtX.Text
            RaiseEvent cmd_Click(lcmdX, New EventArgs)
        End If
        If e.KeyCode = Keys.Enter Then
            SendKeys.Send("{TAB}")
        End If
    End Sub
End Class
#End Region

Public Class AC
    Private pTxt As TextBox, pFcode As String
    Private lappConn As SqlClient.SqlConnection, lTbl, lFieldCode As String
    Public lKey As String

    Public Sub New(ByRef txt As TextBox, ByVal appConn As SqlClient.SqlConnection, ByVal sTbl As String, ByVal sFieldCode As String, ByVal sKey As String)
        pTxt = txt
        pFcode = sFieldCode
        lappConn = appConn
        lTbl = sTbl
        lFieldCode = sFieldCode
        lKey = sKey
        AddHandler pTxt.KeyPress, AddressOf pTxt_KeyPress
        AddHandler pTxt.Leave, AddressOf pTxt_Leave
    End Sub

    Private Sub pTxt_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
        Try
            If pTxt.ReadOnly Or Not pTxt.Enabled Then
                e.Handled = True
                Return
            End If
            'If e.KeyChar = " " Then
            '    e.Handled = True
            '    Return
            'End If
            Dim s As String, Us As Char, cLast As String, nStart, nEnd As Integer
            If e.KeyChar <> ChrW(Keys.Back) And e.KeyChar <> ChrW(Keys.Enter) Then
                e.Handled = True
                If e.KeyChar = "," Then
                    nEnd = GetNEnd()
                    sender.Text = Mid(sender.Text, 1, nEnd) + ", " + Mid(sender.Text, nEnd + 1)
                    sender.SelectionStart += nEnd + 2
                    Return
                End If
            Else
                e.Handled = False
                Return
            End If

            nStart = GetNStart()
            Us = e.KeyChar.ToUpper(e.KeyChar)
            s = Mid(sender.Text, nStart, sender.SelectionStart + 1 - nStart)

            cLast = GetLast(s + Us)

            If Not cLast Is Nothing Then
                sender.Text = Mid(sender.Text, 1, nStart - 1) + s + Us + cLast + Mid(sender.Text, GetNEnd() + 1)
                sender.SelectionStart = nStart + Len(s + Us) - 1
                sender.SelectionLength = Len(cLast)
            Else
                If Us <> " " Then
                    e.Handled = True
                Else
                    e.Handled = False
                End If
            End If
        Catch ex As Exception
            Exit Sub
        End Try
    End Sub

    Private Sub pTxt_Leave(ByVal sender As Object, ByVal e As EventArgs)
        If Trim(sender.Text) <> "" Then
            Dim xds As New DataSet, xdv As New DataView, xs As String = ""
            SQLUtil.SQL.SQLRetrieve(lappConn, "SELECT " + lFieldCode + " FROM " + lTbl + " WHERE " + lKey + " ORDER BY " + lFieldCode, "xds", xds)
            xdv.Table = xds.Tables(0)
            For i As Integer = 1 To GetWordCount(sender.Text, ",")
                xdv.RowFilter = lFieldCode + " = '" + Trim(GetWordNum(sender.Text, i, ",")) + "'"
                If xdv.Count <> 0 Then
                    xs += ", " + Trim(GetWordNum(sender.Text, i, ","))
                End If
            Next
            If xs <> "" Then xs = Mid(xs, 3)
            sender.Text = xs
        End If
    End Sub

    Private Function GetLast(ByVal sBegin As String) As String
        If Trim(sBegin) = "" Then Return Nothing
        'Dim cLast As String = GetValue(lappConn, "SELECT TOP 1 " + lFieldCode + " FROM " + lTbl + " WHERE " + lKey + " AND " + lFieldCode + " LIKE '" + sBegin + "%' ORDER BY " + lFieldCode)
        Dim cLast As String = GetValue(lappConn, lTbl, lFieldCode, lKey + " AND " + lFieldCode + " LIKE '" + sBegin + "%'")
        If Not cLast Is Nothing Then
            Return RTrim(Mid(cLast, Len(RTrim(sBegin)) + 1))
        Else
            Return Nothing
        End If
    End Function

    Private Function GetNStart() As Integer
        Dim nStart0 As Integer = pTxt.SelectionStart
        Dim j As Integer
        For i As Integer = nStart0 To 1 Step -1
            If Mid(pTxt.Text, i, 1) = "," Then
                For j = i + 1 To nStart0
                    If Mid(pTxt.Text, j, 1) <> " " Then
                        Return j
                    End If
                Next
                Return j
            End If
        Next
        Return 1
    End Function

    Private Function GetNEnd() As Integer
        For i As Integer = pTxt.SelectionStart + 1 To Len(RTrim(pTxt.Text))
            If Mid(pTxt.Text, i, 1) = "," Then Return i - 1
        Next
        Return Len(RTrim(pTxt.Text))
    End Function
End Class



